/*
*/

#include "event.h"

#include "display.h"
#include "keypad.h"

static u32	evntc;
static u32	evnts[EVN_MAX>>24];

#ifdef  DEBUG
void Assert(u8* file, u32 line){ 
    printf("Wrong parameters value: file %s on line %d\r\n",file,line);
    while(1){}
}
#endif

static u32 Event(u32 ev){
    u32	e,i,r,s;

    e=ev>>24;
    r=e?evnts[e]:evntc;
    if(r){
	e=r>>24;
	evnts[e]=EVN_NULL;
	if(r==evntc){
	    evntc=EVN_NULL;
	    for(i=EVN_MAX>>24;i;){
		if(s=evnts[--i]){
		    evntc=s;
		    break;
		}
	    }
	}
    }
    return r;
}

u32 GetKey(u32 ev){
    return ev>>16&255;
}

u32 SetEvent(u32 ev){
    u32	e,r;

    e		=ev>>24;
    r		=evnts[e];
    evnts[e]	=
    evntc	=ev;
    return r;
}

u32 WaitEvent(void){
    u32	k;

    if(k=Event(0))
	return k;
    __WFI();
    k=KeypadEvent();
    Display();
    return k;//KeyEvent(k);
}
